<?php

/**
 * This is the model class for table "zy_gift_remaining_number".
 *
 * The followings are the available columns in table 'zy_gift_remaining_number':
 * @property integer $gift_remaining_id
 * @property integer $partners_id
 * @property integer $gift_id
 * @property integer $remaining_number
 * @property string $bak1
 * @property string $bak2
 * @property string $bak3
 * @property integer $is_delete
 * @property string $crate_time
 * @property string $crate_user
 * @property string $update_time
 * @property string $update_user
 */
class GiftRemainingNumber extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return GiftRemainingNumber the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_gift_remaining_number';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('partners_id, gift_id, remaining_number, total,is_delete', 'numerical', 'integerOnly'=>true),
			array('bak1, bak2, bak3', 'length', 'max'=>100),
			array('crate_user, update_user', 'length', 'max'=>50),
			array('crate_time, update_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('gift_remaining_id, partners_id, gift_id, remaining_number,total, bak1, bak2, bak3, is_delete, crate_time, crate_user, update_time, update_user', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'gift_remaining_id' => Yii::t('base','Gift Remaining Id'),
			'partners_id' => Yii::t('base','Partners'),
			'gift_id' => Yii::t('base','Gift Describe'),
			'total'=>Yii::t('base','Gift Total'),
			'remaining_number' => Yii::t('base','Gift Remaining Numbers'),
			'bak1' => 'Bak1',
			'bak2' => 'Bak2',
			'bak3' => 'Bak3',
			'is_delete' => 'Is Delete',
			'crate_time' => 'Crate Time',
			'crate_user' => 'Crate User',
			'update_time' => 'Update Time',
			'update_user' => 'Update User',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$level = yii::app()->user->level();
		$criteria=new CDbCriteria;

		$criteria->compare('gift_remaining_id',$this->gift_remaining_id);
		if($level == 1){
			$criteria->compare('partners_id',$this->partners_id);
		}else{
			$criteria->compare('partners_id',yii::app()->user->id);
		}
		
		$criteria->compare('gift_id',$this->gift_id);
		$criteria->compare('total',$this->total);
		$criteria->compare('remaining_number',$this->remaining_number);
		$criteria->compare('bak1',$this->bak1,true);
		$criteria->compare('bak2',$this->bak2,true);
		$criteria->compare('bak3',$this->bak3,true);
		$criteria->compare('is_delete',$this->is_delete);
		$criteria->compare('crate_time',$this->crate_time,true);
		$criteria->compare('crate_user',$this->crate_user,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('update_user',$this->update_user,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}